/**
 * Created by hanfei on 19/1/28.
 */
//var captrue = document.getElementById("captrue");

JiaoguanUtil = {
    notInterview: function () {
        var uid = IA300_GetHardwareId();
        var memberId = $("input[name='memberId']").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/generateUsbKey.htmls',
            data: {
                "memberId": memberId,
                "sid": uid
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    var message = obj.data.message;
                    //alert(message);
                    var ticket = IA300_CalculateClientHash(message);
                    //alert(ticket);
                    JiaoguanUtil.submitCommonStudentInfo(memberId, ticket);
                } else {
                    alert(obj.message);
                }
            },
            error: function () {
                alert("122非面签提交数据失败,请稍后重试.");
            }
        });
    },
    submitCommonStudentInfo: function (memberId, ticket) {
        var studentId = $("input[name='id']").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/notInterview.htmls',
            data: {
                "memberId": memberId,
                "studentId": studentId,
                "ticket": ticket
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    var result = JSON.parse(obj.data);
                    if (result.code == 200) {
                        alert("生成学员的流水号成功.");
                        $("input[name='studentUid']").val(result.data.data.wwlsh);
                    } else {
                        alert(result.message);
                    }
                }
            },
            error: function () {
                alert("提交学员的基本信息失败,请稍后重试.");
            }
        });
    },
    getencrypt: function () {
        var memberId = $("#memberId").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/getEncrypt.htmls',
            data: {
                "memberId": memberId
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    var random_en = obj.data.message;
                    var random_de = captrue.sGetCheckCode(random_en, 1);
                    JiaoguanUtil.setdecrypt(random_de);
                }
            },
            error: function () {
                alert("获取加密key失败.");
            }
        });
    },
    setdecrypt: function (random_de) {
        var memberId = $("#memberId").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/setDecrypt.htmls',
            data: {
                "memberId": memberId,
                "decrypt": random_de
            },
            dataType: 'JSON',
            success: function (obj) {
                console.log(JSON.stringify(obj));
                //JiaoguanUtil.editStudent();
            },
            error: function () {
                alert("设置解密key失败.");
            }
        });
    },
    submitImageInfo: function (flag, zpath) {
        var memberId = $("#memberId").val();
        var studentUId = $("#studentUid").val();
        IA300_CheckExist();
        //var retVal = IA300_CheckPassword('24680!#%&(tmri');
        var uid = IA300_GetHardwareId();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/generateUsbKey.htmls?t=' + Date.parse(new Date()),
            data: {
                "memberId": memberId,
                "sid": uid
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    var message = obj.data.message;
                    //alert(message);
                    var ticket = IA300_CalculateClientHash(message);
                    //alert(ticket);
                    //var uploadUrl = "/OpenData/Police/sUpLoadImageEx2?bz=2&wwlsh=243001901038700439";
                    // captrue.sGetImageBase64(3);
                    JiaoguanUtil.generateParam(zpath, flag, studentUId, ticket, memberId);
                } else {
                    alert(obj.message);
                }
            },
            error: function () {
                alert("上传学员图片失败.");
            }
        });
    },
    generateParam: function (zpPath, flag, studentUId, ticket, memberId) {
        //var uploadUrl = "/drv/sl/image/save?bz=" + flag + "&wwlsh=" + studentUId + "&ticket=" + ticket;
        var memberId = $("#memberId").val();
        var uploadUrl = "/caiwu/web/studentimg/upload122.htmls?memberId=" + memberId;
        //var uploadUrl = "/OpenData/Police/sUpLoadImageEx2?bz=" + flag + "&wwlsh=" + studentUId;
        var ipAndPort = JiaoguanUtil.getIpAndPort();
        var str = captrue.sUpLoadImageEx2(zpPath, ipAndPort['ip'], ipAndPort['port'], uploadUrl, true, false);
        var result = JSON.parse(str);
        if (result.success) {
            var para1 = result.data.para1;
            var para2 = result.data.para2;
            var jsessionId = result.data.jsessionId;
            JiaoguanUtil.uploadImage(jsessionId, flag, studentUId, ticket, para1, para2);
        } else {
            alert("图片提交122失败.");
        }
    },
    editStudent: function (memberId, studentUid) {
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/editStudent.htmls?t=' + Date.parse(new Date()),
            data: {
                "memberId": memberId,
                "studentUid": studentUid
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    JiaoguanUtil.notInterview();
                } else {
                    alert(obj.message);
                }
            },
            error: function () {
                alert("获取上传学员信息失败.");
            }
        });
    },
    checkCertNo: function (memberId) {
        var certNo = $("input[name='certNo']").val();
        var certType = "A";
        $.ajax({
            type: 'POST',
            url: HuiqianUtil.getWebRootPath() + '/web/data/checkCertNo.htmls',
            data: {
                "memberId": memberId,
                "certType": certType,
                "certNo": certNo
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    JiaoguanUtil.notInterview();
                } else {
                    alert(obj.message);
                }
            },
            error: function () {
                alert("获取上传学员信息失败.");
            }
        });
    },
    getIpAndPort: function () {
        var url = window.location.href;
        var pathname = window.document.location.pathname;
        var index = url.indexOf(pathname);
        var rooturl = url.substring(0, index);
        rooturl = rooturl.replace("http://", "");
        rooturl = rooturl.replace("https://", "");
        var index1 = rooturl.indexOf(":");
        var ip;
        var port;
        if (index1 < 0) {
            ip = rooturl;
            port = 80;
        } else {
            ip = rooturl.substring(0, index1);
            port = rooturl.substring(index1 + 1);
        }
        return {"ip": ip, "port": port};
    },
    uploadImage: function (jsessionId, flag, studentUId, ticket, para1, para2) {
        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8886/picture/upload',
            data: {
                "jsessionId": jsessionId,
                "studentCode": studentUId,
                "type": flag,
                "ticket": ticket,
                "para1": para1,
                "para2": para2
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    if (obj.data.startWith("1")) {
                        alert("图片提交成功.");
                        $("#take_pic").modal('hide');
                        JiaoguanUtil.downloadImage(flag, studentUId);
                    } else {
                        alert("122省网环境不稳定，请稍后重试.");
                    }
                } else {
                    alert("122省网环境不稳定，请稍后重试.");
                }
            },
            error: function () {
                alert("122省网环境不稳定，请稍后重试.");
            }
        });
    },
    downloadImage: function (type, studentUId) {
        var memberId = $("#memberId").val();
        var studentId = $("#studentId").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/downloadImage.htmls?t=' + Date.parse(new Date()),
            data: {
                "memberId": memberId,
                "studentId": studentId,
                "type": type,
                "studentUid": studentUId
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    JiaoguanUtil.loadImage(type, studentUId);
                }
            },
            error: function () {
                alert("下载学员图片失败.");
            }
        });
    },
    loadImage: function (type, studentUid) {
        var memberId = $("#memberId").val();
        var studentId = $("#studentId").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/loadImage.htmls?t=' + Date.parse(new Date()),
            data: {
                "memberId": memberId,
                "studentId": studentId,
                "type": type,
                "studentUid": studentUid
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    $("#photo" + type).attr("src", HuiqianUtil.getWebRootPath() + "/download/" + obj.data + "?t=" + Date.parse(new Date()));
                    $("#pai_tip_" + type).hide();
                    $("#photo" + type).show();
                }
            },
            error: function () {
                alert("加载学员图片失败.");
            }
        });
    },
    commitStudentTo122: function () {
        var memberId = $("#memberId").val();
        var studentId = $("#studentId").val();
        var studentUid = $("#studentUid").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/commit.htmls',
            data: {
                "memberId": memberId,
                "studentUid": studentUid,
                "studentId": studentId
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    var result = JSON.parse(obj.data);
                    if (result.code == 200) {
                        alert("学员预录入成功.");
                        //JiaoguanUtil.updateStudentRegTime();
                    } else {
                        alert(result.message);
                    }
                }
            },
            error: function () {
                alert("学员预录入提交失败,请稍后再试.");
            }
        });
    },
    updateStudentRegTime: function () {
        var studentId = $("#studentId").val();
        $.ajax({
            type: 'GET',
            url: HuiqianUtil.getWebRootPath() + '/web/data/updateRegTime.htmls',
            data: {
                "studentId": studentId
            },
            dataType: 'JSON',
            success: function (obj) {
                if (obj.success) {
                    console.log("更新学员" + studentId + "的注册时间成功.");
                }
            },
            error: function () {
                alert("更新学员注册时间失败,请稍后再试.");
            }
        });
    }
}